package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.constant.Constant;
import com.ygqh.baby.mapper.YgWeixinMenuDayMapper;
import com.ygqh.baby.po.YgWeixinMenuDay;
import com.ygqh.baby.po.YgWeixinMenuDayExample;
import com.ygqh.baby.service.YgWeixinMenuDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author admin
 */
@Service
public class YgWeixinMenuDayServiceImpl implements YgWeixinMenuDayService {

	private final static ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(3);

	@Autowired
	private YgWeixinMenuDayMapper ygWeixinMenuDayMapper;

	@Override
	public List<YgWeixinMenuDay> find() {
		YgWeixinMenuDayExample example = new YgWeixinMenuDayExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWeixinMenuDayMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgWeixinMenuDayExample example = new YgWeixinMenuDayExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWeixinMenuDayMapper.countByExample(example);
	}

	@Override
	public YgWeixinMenuDay findById(Long id) {
		return ygWeixinMenuDayMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgWeixinMenuDay ygWeixinMenuDay) {
		return ygWeixinMenuDayMapper.insertSelective(ygWeixinMenuDay);
	}

	@Override
	public int update(YgWeixinMenuDay ygWeixinMenuDay) {
		return ygWeixinMenuDayMapper.updateByPrimaryKeySelective(ygWeixinMenuDay);
	}

	@Override
	public ResultSet<YgWeixinMenuDay> search(QueryInfo queryInfo, String q) {
		int count = ygWeixinMenuDayMapper.countSuper(q);
		if (count == 0) {
			return new ResultSet<>();
		}
		List<YgWeixinMenuDay> list = ygWeixinMenuDayMapper.selectSuper(queryInfo, q);
		return new ResultSet<>(count, list);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgWeixinMenuDayExample example = new YgWeixinMenuDayExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgWeixinMenuDay record = new YgWeixinMenuDay();
		record.setStatus(DataStatus.Delete);
		return ygWeixinMenuDayMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgWeixinMenuDay ygWeixinMenuDay) {
		int r;
		if (ygWeixinMenuDay.getId() != null) {
			r = this.update(ygWeixinMenuDay);
		} else {
			r = this.save(ygWeixinMenuDay);
		}
		return r;
	}

	@Override
	public void saveMenuClick(String openId, String url) {
		saveMenuClick(openId, url, Constant.YIIGOO_PLATFORM_NO);
	}

	@Override
	public void saveMenuClick(String openId, String url, String platformNo) {
		EXECUTOR_SERVICE.execute(() -> {
			String menuName;
			if (url.contains("mall/index")) {
				menuName = "壹果商城";
			} else if (url.contains("static.html")) {
				menuName = "女王专区";
			} else if (url.contains("bd/index.html")) {
				menuName = "联系方式";
			} else if (url.contains("mall/h5Chat.html")) {
				menuName = "联系客服";
			} else if (url.contains("return/returnList.html")) {
				menuName = "退换货";
			} else {
				menuName = url;
			}
			YgWeixinMenuDay menu = new YgWeixinMenuDay();
			menu.setOpenId(openId);
			menu.setCreateTime(new Date());
			menu.setClickDate(menu.getCreateTime());
			menu.setMenuName(menuName);
			menu.setStatus(DataStatus.Valid);
			menu.setPlatformNo(platformNo);
			save(menu);
		});
	}

	@Override
	public List<String> getOpenId(Date startDate) {
		return getOpenId(startDate, Constant.YIIGOO_PLATFORM_NO);
	}

	@Override
	public List<String> getOpenId(Date startDate, String platformNo) {
		return ygWeixinMenuDayMapper.selectOpenId(startDate, platformNo);
	}
}
